<?php

namespace app\common\model\product;

use app\shop\model\product\BrandModel;
use think\facade\Cache;
use app\common\model\BaseModel;
use think\facade\Db;

/**
 * 产品分类模型
 */
class ProductBrandRelation extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'product_brand_relathion';

    public static function listDataByUser($where, $params, $with = [], $sort_num, $supplier_id)
    {
        $where = BaseModel::disposeWhere($where);
        $model = new static();
        $model = $model->with(['brand', 'product' => function ($query) {
            $query->with('image.file');
        }])->withJoin(['productTemp'])
            ->where('productTemp.is_delete', '=', 0)
            ->where('productTemp.audit_status', '=', 10)
            ->where('productTemp.product_status', '=', 10)
            ->where('product_brand_relation.brand_id', '=', $where['brand_id'])
            ->whereIn('product_brand_relation.shop_supplier_id', $supplier_id);
        $list = $model->paginate($params);
        return $list;
    }

    public static function listDataTemp($where, $params, $with = [], $sort_num, $supplier_id)
    {
        $where = BaseModel::disposeWhere($where);
        $model = new static();
        $model = $model->with(['brand', 'product' => function ($query) {
            $query->with('image.file');
        }])->withJoin(['productTemp'])
            ->where('productTemp.is_delete', '=', 0)
            ->where('productTemp.audit_status', '=', 10)
            ->where('productTemp.product_status', '=', 10)
            ->where('product_brand_relation.brand_id', '=', $where['brand_id']);
        $list = $model->paginate($params);
        return $list;
    }

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort_num)
    {

        $where = BaseModel::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort_num)) {
            $model = $model->order($sort_num);
        } else {
            $model = $model->order(BaseModel::baseOrder());
        }
        $model = BaseModel::assWhere($model, $where);
        $list = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $model = new static();
        $where['app_id'] = self::$app_id;
        $data = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [])
    {
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = BaseModel::assWhere($model, $where);
        $model = $model->order(BaseModel::baseOrder());
        $list = $model->select()->toArray();
        return $list;
    }

    public function brand()
    {
        return $this->hasOne('app\\common\\model\\product\\Brand', 'id', 'brand_id');
    }

    public function product()
    {
        return $this->hasOne('app\\common\\model\\product\\Product', 'product_id', 'product_id');
    }

    public function productTemp()
    {
        return $this->hasOne('app\\common\\model\\product\\Product', 'product_id', 'product_id');
    }
}
